# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=tblite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.4.0
pkgrel=1
pkgdesc="Light-weight tight-binding framework. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64')
url='https://tblite.readthedocs.io/'
msys2_repository_url="https://github.com/tblite/tblite"
license=('spdx:LGPL-3.0-or-later')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-dftd4"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-mctc-lib"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-s-dftd3"
         "${MINGW_PACKAGE_PREFIX}-toml-f")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-mstore")
source=("https://github.com/tblite/tblite/releases/download/v${pkgver}/tblite-${pkgver}.tar.xz"
        "001-fix-install-dll.patch")
sha256sums=('5c2249b568bfd3b987d3b28f2cbfddd5c37f675b646e17c1e750428380af464b'
            '45f7108ef51ab31ca0956e0ce7b896a0e98cea2a32bb1240d349cf2f3c202418')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DWITH_API=ON \
      -DWITH_OpenMP=ON \
      -DWITH_TESTS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake -DWITH_TESTS=ON -S"${_realname}-${pkgver}" -B"build-${MSYSTEM}"
  cmake --build "build-${MSYSTEM}"

  ctest --test-dir "build-${MSYSTEM}" --output-on-failure
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"
}
